/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View.planificacion;

import Algorithm.* ;
import DAO.EnvioPedidoDAO;
import DAO.PlanificacionDAO;
import Model.* ;
import Presenter.EnvioPedidoPresenter;
import Principals.Constantes;
import Principals.Main;
import Principals.Utils;
import java.awt.Color;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author sebastian
 */
public class Frm_Mapa extends javax.swing.JDesktopPane {

    private DefaultTableModel mdlPedidos;
    private DefaultTableModel mdlVehiculos;
    private DefaultTableModel mdlEmpleados;
    private DefaultTableModel mdlEnvioPedido ;
    private List<PuntoMapa> lstPuntos ;
    private List<EnvioPedidoAlg> lstEnvioPedidosAlg ;
    private Color BG_COLOR ;
    private JButton puntos[] ;
    private List<JFormattedTextField> caminosEnvio[] ;
    private JButton chofer[] ;
    /**
     * Creates new form Frm_Mapa
     */
    public Frm_Mapa() {
        Main.usrSesion.refresh() ;
        EnvioPedidoPresenter.actualizarEnvioPedido() ;
        mdlPedidos = new DefaultTableModel() ;
        mdlVehiculos = new DefaultTableModel() ;
        mdlEmpleados = new DefaultTableModel() ;
        mdlEnvioPedido = new DefaultTableModel() ;
        initComponents() ;
        cargarPuntos() ;
        //se jalan de base de datos todos los pedidos pendientes
        //primero se actualizan
        Algoritmo objAlgoritmoGenetico = new Algoritmo() ;
        try {
            objAlgoritmoGenetico.load() ;
        } catch (Exception ex) {
            Logger.getLogger(Frm_PlanificarPedidos.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        List<EnvioPedido> lstEnvioPedido = EnvioPedidoDAO.buscarEnvioPedidoEnRuta() ;
        lstEnvioPedidosAlg = new ArrayList<EnvioPedidoAlg>();
        //llenado el envio pedio alg
        for(int i=0; i<lstEnvioPedido.size();i++){
            EnvioPedidoAlg objEnvioPedidoAlg;
            objEnvioPedidoAlg = lstEnvioPedido.get( i ).toEnvioPedidoAlg() ;
            lstEnvioPedidosAlg.add(objEnvioPedidoAlg);
        }
        armarRutas() ;
        initChofer( lstEnvioPedidosAlg ) ;
        cargarDatos( lstEnvioPedidosAlg );
        pintarMapa( 0 ) ;
    }

    public Frm_Mapa( List<EnvioPedidoAlg> envPedidos ){
        Main.usrSesion.refresh() ;
        EnvioPedidoPresenter.actualizarEnvioPedido() ;
        mdlPedidos = new DefaultTableModel() ;
        mdlVehiculos = new DefaultTableModel() ;
        mdlEmpleados = new DefaultTableModel() ;
        mdlEnvioPedido = new DefaultTableModel() ;
        lstEnvioPedidosAlg = envPedidos ;
        initComponents() ;
        cargarPuntos() ;
        armarRutas() ;
        initChofer( envPedidos ) ;
        cargarDatos( envPedidos ) ;
        pintarMapa( 0 ) ;
    }
    
    private void initChofer( List<EnvioPedidoAlg> envPedidos ){
        int tam = envPedidos.size() ;
        chofer = new JButton[ tam ] ;
        for(int i = 0 ; i < tam ; i++){
            chofer[ i ] = new JButton() ;
            chofer[ i ].setName( "chofer" + i  ) ;
            chofer[ i ].setVisible( false ) ;
            chofer[ i ].setSize( 15 , 15 ) ;
            chofer[ i ].setForeground( Color.BLUE ) ;
            chofer[ i ].setBackground( Color.BLUE ) ;
            Point p = calcularPosicion( envPedidos.get( i ) ) ;
            chofer[ i ].setLocation( p ) ;
            chofer[ i ].setToolTipText( "Vehiculo #" + envPedidos.get( i ).getVehiculo().getIdVehiculo() ) ;
            pnlMapa.add( chofer[ i ] ) ;
        }
    }
    
    private void cargarDatos( List<EnvioPedidoAlg> envPedidos ){
        cbxEnvioPedido.removeAllItems() ;
        cbxEnvioPedido.addItem( "Todos" ) ;
        for( EnvioPedidoAlg envPed : envPedidos ) cbxEnvioPedido.addItem( "Vehiculo #" + envPed.getVehiculo().getIdVehiculo() ) ;
        cbxEnvioPedido.setSelectedIndex( 0 ) ;
    }
    
    private Point calcularPosicion( EnvioPedidoAlg envPed ){
        int idxEnvioPedido = -1 ;
        for(int i = 0 ; i < lstEnvioPedidosAlg.size() && idxEnvioPedido == -1 ; i++) 
            if( envPed.getVehiculo().getIdVehiculo() == lstEnvioPedidosAlg.get(i).getVehiculo().getIdVehiculo() )
                idxEnvioPedido = i ;
        System.out.println( "envio = " + idxEnvioPedido ) ;
        Date now = new Date() ;
        Date salida = envPed.getFechaSalida() ;
        int idVel = ( envPed.getVehiculo().getTipoVehiculo().getValorString().equalsIgnoreCase( Constantes.NOMBRE_MOTO_BD ) ? Constantes.IDX_VPROM_MOTO : Constantes.IDX_VPROM_AUTO ) ;
        int vel = PlanificacionDAO.getVariableSistemaById( idVel ).getValorNumerico().intValue() ;
//        System.out.println( "salida = " + salida.toString() ) ;
//        System.out.println( "now = " + now.toString() ) ;
        double tiempo = ( (double)Utils.diffHourMinutes( salida , now ) /*- 5 * envPed.getPedidos().size()*/ )/ 60 ;
//        System.out.println( "velocidad = " + vel + " , tiempo = " + tiempo ) ;
        double distanciaRecorrida = vel * tiempo , dist = 0 ;
//        System.out.println("distancia = " + distanciaRecorrida ) ;
        int idxCamino = 0 ;
        List<Camino> lstCaminos = envPed.getrCamino() ;
        Point p = new Point() ;
        for( Camino cam : lstCaminos ){
            dist += cam.getDistancia() ;
//            System.out.println("D = " + dist ) ;
            if( dist > distanciaRecorrida ){
//                System.out.println("asdifuhasuidfhusadfluasidfasdfuasdfiuasdf") ;
                caminosEnvio[ idxEnvioPedido ].get( idxCamino ).setVisible(false);
                p = caminosEnvio[ idxEnvioPedido ].get( idxCamino ).getLocation() ;
                break ;
            }
            idxCamino++ ;
        }
//        System.out.println( "dist = " + distanciaRecorrida ) ;
        return p ;
    }
    
    private void armarRutas(){
        int tam = lstEnvioPedidosAlg.size() ;
        caminosEnvio = new ArrayList[ tam ] ;
        for(int i = 0 ; i < tam ; i++) caminosEnvio[ i ] = new ArrayList<JFormattedTextField>() ;
        int numColores = Constantes.NUM_COLORES ;
        for( int i = 0 ; i < lstEnvioPedidosAlg.size() ; i++){
            List<Camino> lstCamino = lstEnvioPedidosAlg.get( i ).getrCamino() ;
            BG_COLOR = Constantes.colores[ i % numColores ] ;
            for( Camino cam : lstCamino ){
                PuntoMapa ini = cam.getPuntoMapaByPuntoInicial() ;
                PuntoMapa fin = cam.getPuntoMapaByPuntoFinal() ;
                caminosEnvio[ i ].add( armarCamino( ini , fin ) ) ;
            }
        }
    }
    
    private void pintarMapa( int idxSelected ){
        if( idxSelected > 0 ){
            EnvioPedidoAlg envPed = lstEnvioPedidosAlg.get( idxSelected - 1 ) ;
            List<Pedido> lstPedido = envPed.getPedidos() ;
            for( Pedido ped : lstPedido ) colorPuntoPedido( ped ) ;
        }else{
            for( EnvioPedidoAlg envPed : lstEnvioPedidosAlg ){
                List<Pedido> lstPedido = envPed.getPedidos() ;
                for( Pedido ped : lstPedido ) colorPuntoPedido( ped ) ;
            }
        }
        graficarCaminos( idxSelected ) ;
        pintarChofer( idxSelected ) ;
    }
    
    private void pintarChofer( int idxSelected ){
        if( idxSelected-- > 0 ){
            chofer[ idxSelected ].setVisible( true ) ;
        }else{
            for(int i = 0 ; i < chofer.length ; i++) chofer[ i ].setVisible( true ) ;
        }
    }
    
    private void colorPuntoPedido( Pedido ped ){
        PuntoMapa punto = ped.getPuntoMapa() ;
        JButton but = puntos[ punto.getLatitud() * Utils.ANCHO_MAPA + punto.getLongitud() ] ;
        but.setForeground( Color.RED ) ;
        but.setBackground( Color.RED ) ;
        but.setToolTipText( "Pedido " + ped.getIdPedido() ) ;
    }

    private void llenarTabla(List<EnvioPedidoAlg> lstEnvioPedidoAlg){
        mdlPedidos.setNumRows(0);
        mdlPedidos = (DefaultTableModel) tblPedidos.getModel() ;
        
        mdlEmpleados.setNumRows(0);
        mdlEmpleados = (DefaultTableModel) tblEmpleados.getModel() ;
        
        mdlVehiculos.setNumRows( 0 ) ;
        mdlVehiculos = (DefaultTableModel) tblVehiculos.getModel() ;
        
        mdlEnvioPedido.setNumRows( 0 ) ;
        mdlEnvioPedido = (DefaultTableModel) tblEnvioPedido.getModel() ;
        
        int idxPedido = 0 ;
        int idxVehiculo = 0 ;
        int idxEmpleado = 0 ;
        int idxEnvioPedido = 0 ;
        for (int i=0; i<lstEnvioPedidoAlg.size();i++){
            /* ======== ACTUALIZAR ENVIO PEDIDO ======== */
            EnvioPedidoAlg envPed = lstEnvioPedidoAlg.get( i ) ;
            Object[] envio = new Object[ 4 ] ;
            envio[ 0 ] = envPed.getVehiculo().getIdVehiculo() ;
            envio[ 1 ] = envPed.getUtilidad() ;
            envio[ 2 ] = Utils.formatDateWithHours( envPed.getFechaSalida() ) ;
            envio[ 3 ] = Utils.formatDateWithHours( envPed.getFechaRegreso() ) ;
            mdlEnvioPedido.insertRow( idxEnvioPedido++ , envio ) ;
            
            /* ======== ACTUALIZAR TABLA VEHICULOS ======== */
            Vehiculo veh = lstEnvioPedidoAlg.get( i ).getVehiculo() ;
            Object[] vehiculo = new Object[ 3 ] ;
            vehiculo[ 0 ] = veh.getIdVehiculo() ;
            vehiculo[ 1 ] = veh.getTipoVehiculo().getValorString() ;
            vehiculo[ 2 ] = veh.getPlaca() ;
            mdlVehiculos.insertRow(idxVehiculo++,vehiculo);
            
            /* ======== ACTUALIZAR TABLA EMPLEADO ======== */
            Empleado emp = lstEnvioPedidoAlg.get( i ).getEmpleado() ;
            Object[] empleado = new Object[ 6 ] ;
            empleado[ 0 ] = veh.getIdVehiculo() ;
            empleado[ 1 ] = emp.getNombres() ;
            empleado[ 2 ] = emp.getApellidoPaterno() ;
            empleado[ 3 ] = emp.getApellidoMaterno() ;
            empleado[ 4 ] = emp.getCorreo() ;
            empleado[ 5 ] = emp.getTelefono() ;
            mdlEmpleados.insertRow(idxEmpleado++,empleado);
            
            /* ======== ACTUALIZAR TABLA PEDIDOS ======== */
            List<Pedido> lstPedidos = lstEnvioPedidoAlg.get( i ).getPedidos() ;
            for(int j=0; j<lstPedidos.size();j++){
                Object[] fila = new Object[ 4 ] ;
                fila[ 0 ] = envPed.getVehiculo().getIdVehiculo() ;
                fila[ 1 ] = lstPedidos.get( j ).getIdPedido() ;
                fila[ 2 ] = Utils.formatDateWithHours(lstPedidos.get( j ).getFechaEntrega());
                fila[ 3 ] = lstEnvioPedidoAlg.get( i ).getEmpleado().getIdEmpleado() ;
                mdlPedidos.insertRow(idxPedido, fila);
                idxPedido++;
            }
        }
    }

    private JFormattedTextField armarCamino( PuntoMapa ini , PuntoMapa fin ){
        if( ini.getLatitud() > fin.getLatitud() ){
            PuntoMapa aux = ini ;
            ini = fin ;
            fin = aux ;
        }
        if( ini.getLongitud()> fin.getLongitud() ){
            PuntoMapa aux = ini ;
            ini = fin ;
            fin = aux ;
        }
        JFormattedTextField cam = new JFormattedTextField() ;
        cam.setEditable( false ) ;
//        cam.setBackground( new Color( BG_COLOR , BG_COLOR , BG_COLOR ) ) ;
        cam.setBackground( BG_COLOR ) ;
        Point pIni = new Point( ini.getLongitud() * 48 + 15 , ini.getLatitud() * 40 + 30 ) ;
        Point pFin = new Point( fin.getLongitud() * 48 + 15 , fin.getLatitud() * 40 + 30 ) ;
        cam.setLocation( pIni ) ;
        cam.setVisible( false ) ;
        if( pIni.y == pFin.y )
            cam.setSize( 55 , 10 ) ;
        else
            cam.setSize( 10 , 50 ) ;
        pnlMapa.add( cam ) ;
        return cam ;
    }
    
    private void graficarCaminos( int idxSelected ){
        if( idxSelected-- > 0 ){
            for(int i = 0 ; i < caminosEnvio[ idxSelected ].size() ; i++){
                caminosEnvio[ idxSelected ].get( i ).setVisible( true ) ;
            }
        }else{
            for(int i = 0 ; i < caminosEnvio.length ; i++){
                for(int j = 0 ; j < caminosEnvio[ i ].size() ; j++){
                    caminosEnvio[ i ].get( j ).setVisible( true ) ;
                }
            }
        }
    }
    
    private void limpiarMapa(){
        for(int i = 0 ; i < caminosEnvio.length ; i++)
            for(int j = 0 ; j < caminosEnvio[ i ].size() ; j++)
                caminosEnvio[ i ].get( j ).setVisible( false ) ;
        for(int i = 0 ; i < puntos.length ; i++) puntos[ i ].setBackground( new Color( 214 , 217 , 223 ) ) ;
        for(int i = 0 ; i < chofer.length ; i++) chofer[ i ].setVisible( false ) ;
    }
    
    private void cargarPuntos(){
        lstPuntos = PlanificacionDAO.getPuntosMapa() ;
        puntos = new JButton[ lstPuntos.size() ] ;
        for(int i = 0 ; i < lstPuntos.size() ; i++){
            PuntoMapa p = lstPuntos.get( i ) ;
            puntos[ i ] = new JButton() ;
            puntos[ i ].setLocation( p.getLongitud() * 48 + 15 , p.getLatitud() * 40 + 30 ) ;
            puntos[ i ].setName( "punto" + i  ) ;
            puntos[ i ].setVisible( true ) ;
            puntos[ i ].setSize( 10 , 10 ) ;
            pnlMapa.add( puntos[ i ] ) ;
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlPrincipal = new javax.swing.JPanel();
        pnlMapa = new javax.swing.JPanel();
        cbxEnvioPedido = new javax.swing.JComboBox();
        pnlDatosEmpleado = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblEmpleados = new javax.swing.JTable();
        pnlDatosVehiculo = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblVehiculos = new javax.swing.JTable();
        pnlProximasEntregas = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblPedidos = new javax.swing.JTable();
        pnlDatosVehiculo1 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tblEnvioPedido = new javax.swing.JTable();

        pnlPrincipal.setPreferredSize(new java.awt.Dimension(1000, 590));

        pnlMapa.setBorder(javax.swing.BorderFactory.createTitledBorder("Mapa"));

        org.jdesktop.layout.GroupLayout pnlMapaLayout = new org.jdesktop.layout.GroupLayout(pnlMapa);
        pnlMapa.setLayout(pnlMapaLayout);
        pnlMapaLayout.setHorizontalGroup(
            pnlMapaLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 941, Short.MAX_VALUE)
        );
        pnlMapaLayout.setVerticalGroup(
            pnlMapaLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 382, Short.MAX_VALUE)
        );

        cbxEnvioPedido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxEnvioPedidoActionPerformed(evt);
            }
        });

        pnlDatosEmpleado.setBorder(javax.swing.BorderFactory.createTitledBorder("Empleados"));

        tblEmpleados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Vehiculo", "Nombre", "Ap. Paterno", "Ap. Materno", "Email", "Telefono"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tblEmpleados);

        org.jdesktop.layout.GroupLayout pnlDatosEmpleadoLayout = new org.jdesktop.layout.GroupLayout(pnlDatosEmpleado);
        pnlDatosEmpleado.setLayout(pnlDatosEmpleadoLayout);
        pnlDatosEmpleadoLayout.setHorizontalGroup(
            pnlDatosEmpleadoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlDatosEmpleadoLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 669, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnlDatosEmpleadoLayout.setVerticalGroup(
            pnlDatosEmpleadoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlDatosEmpleadoLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                .addContainerGap())
        );

        pnlDatosVehiculo.setBorder(javax.swing.BorderFactory.createTitledBorder("Vehiculos"));

        tblVehiculos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Tipo", "Placa"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblVehiculos.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tblVehiculos);

        org.jdesktop.layout.GroupLayout pnlDatosVehiculoLayout = new org.jdesktop.layout.GroupLayout(pnlDatosVehiculo);
        pnlDatosVehiculo.setLayout(pnlDatosVehiculoLayout);
        pnlDatosVehiculoLayout.setHorizontalGroup(
            pnlDatosVehiculoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlDatosVehiculoLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 277, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlDatosVehiculoLayout.setVerticalGroup(
            pnlDatosVehiculoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlDatosVehiculoLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 96, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlProximasEntregas.setBorder(javax.swing.BorderFactory.createTitledBorder("Pedidos"));

        tblPedidos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Vehiculo", "ID", "Hora de Entrega", "Empleado"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblPedidos.getTableHeader().setReorderingAllowed(false);
        jScrollPane3.setViewportView(tblPedidos);

        org.jdesktop.layout.GroupLayout pnlProximasEntregasLayout = new org.jdesktop.layout.GroupLayout(pnlProximasEntregas);
        pnlProximasEntregas.setLayout(pnlProximasEntregasLayout);
        pnlProximasEntregasLayout.setHorizontalGroup(
            pnlProximasEntregasLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlProximasEntregasLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnlProximasEntregasLayout.setVerticalGroup(
            pnlProximasEntregasLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlProximasEntregasLayout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );

        pnlDatosVehiculo1.setBorder(javax.swing.BorderFactory.createTitledBorder("EnvioPedido"));

        tblEnvioPedido.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Vehiculo", "Utilidad", "Fecha Salida", "Fecha Regreso"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblEnvioPedido.getTableHeader().setReorderingAllowed(false);
        jScrollPane4.setViewportView(tblEnvioPedido);

        org.jdesktop.layout.GroupLayout pnlDatosVehiculo1Layout = new org.jdesktop.layout.GroupLayout(pnlDatosVehiculo1);
        pnlDatosVehiculo1.setLayout(pnlDatosVehiculo1Layout);
        pnlDatosVehiculo1Layout.setHorizontalGroup(
            pnlDatosVehiculo1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlDatosVehiculo1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 393, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnlDatosVehiculo1Layout.setVerticalGroup(
            pnlDatosVehiculo1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlDatosVehiculo1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 85, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout pnlPrincipalLayout = new org.jdesktop.layout.GroupLayout(pnlPrincipal);
        pnlPrincipal.setLayout(pnlPrincipalLayout);
        pnlPrincipalLayout.setHorizontalGroup(
            pnlPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlPrincipalLayout.createSequentialGroup()
                .add(pnlMapa, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(pnlPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(pnlPrincipalLayout.createSequentialGroup()
                        .add(79, 79, 79)
                        .add(cbxEnvioPedido, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 144, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, pnlPrincipalLayout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(pnlPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(pnlDatosVehiculo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(pnlProximasEntregas, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
            .add(pnlPrincipalLayout.createSequentialGroup()
                .add(90, 90, 90)
                .add(pnlDatosEmpleado, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(pnlDatosVehiculo1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pnlPrincipalLayout.setVerticalGroup(
            pnlPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, pnlPrincipalLayout.createSequentialGroup()
                .add(pnlPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(pnlPrincipalLayout.createSequentialGroup()
                        .add(11, 11, 11)
                        .add(cbxEnvioPedido, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(pnlDatosVehiculo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(pnlProximasEntregas, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(pnlPrincipalLayout.createSequentialGroup()
                        .addContainerGap()
                        .add(pnlMapa, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(pnlPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(pnlPrincipalLayout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(pnlDatosEmpleado, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, pnlPrincipalLayout.createSequentialGroup()
                        .add(13, 13, 13)
                        .add(pnlDatosVehiculo1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlPrincipal, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1284, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pnlPrincipal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cbxEnvioPedidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxEnvioPedidoActionPerformed
        //        System.out.println( cbxEnvioPedido.getSelectedItem() ) ;
        List<EnvioPedidoAlg> envPeds = new ArrayList<EnvioPedidoAlg>() ;
        int idxSelected = cbxEnvioPedido.getSelectedIndex() ;
        if( idxSelected == 0 ) envPeds = lstEnvioPedidosAlg ;
        else envPeds.add( lstEnvioPedidosAlg.get( idxSelected - 1 ) ) ;
        limpiarMapa() ;
        llenarTabla( envPeds ) ;
        pintarMapa( idxSelected ) ;
    }//GEN-LAST:event_cbxEnvioPedidoActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbxEnvioPedido;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JPanel pnlDatosEmpleado;
    private javax.swing.JPanel pnlDatosVehiculo;
    private javax.swing.JPanel pnlDatosVehiculo1;
    private javax.swing.JPanel pnlMapa;
    private javax.swing.JPanel pnlPrincipal;
    private javax.swing.JPanel pnlProximasEntregas;
    private javax.swing.JTable tblEmpleados;
    private javax.swing.JTable tblEnvioPedido;
    private javax.swing.JTable tblPedidos;
    private javax.swing.JTable tblVehiculos;
    // End of variables declaration//GEN-END:variables
}
